package teaching.tools;

import static org.math.array.DoubleArray.increment;

import java.util.HashMap;

import javax.swing.JFrame;

import org.math.plot.Plot3DPanel;

import teaching.data.LearningSet;
import teaching.data.Vecteur;
import teaching.models.ScoringFunction;

public class Draw 
{
	public static double computeAccuracy(LearningSet learningset,ScoringFunction scoringFunction,double threshold)
	{
		int nbok=0;
		for(int i=0;i<learningset.size();i++)
		{
			double real=learningset.getLabel(i)==1 ? 1 : -1;
			double predicted=scoringFunction.getScore(learningset.getVector(i)) >= threshold ? +1 : -1;
			if (real==predicted) nbok++;
		}
		return(nbok/(double)(learningset.size()));
	}
	
	
	/**
	 * Permet de dessiner une frontiere de decision et un corpus
	 */
	public static void draw(LearningSet learningset,ScoringFunction scoringFunction,int precision)
	{
		assert(learningset.size()>0);
		Vecteur v=learningset.getVector(0);
		double xmin=v.getValue(0);
		double xmax=v.getValue(0);
		double ymin=v.getValue(1);
		double ymax=v.getValue(1);
		
		HashMap<Double,Integer> nb_by_category=new HashMap<Double,Integer>();
		
		for(int i=0;i<learningset.size();i++)
		{
			v=learningset.getVector(i);
			if (v.getValue(0)<xmin) xmin=v.getValue(0);
			if (v.getValue(0)>xmax) xmax=v.getValue(0);
			if (v.getValue(1)<ymin) ymin=v.getValue(1);
			if (v.getValue(1)>ymax) ymax=v.getValue(1);
			double cat=learningset.getLabel(i);
			if (!nb_by_category.containsKey(cat)) nb_by_category.put(cat,0);
			nb_by_category.put(cat,nb_by_category.get(cat)+1);			
		}
		
		double ampX=xmax-xmin;
		double ampY=ymax-ymin;
		/*xmin-=ampX/10.0;
		xmax+=ampX/10.0;
		ymin-=ampY/10.0;
		ymax+=ampY/10.0;*/
		
		Plot3DPanel plot = new Plot3DPanel("SOUTH");
		for(double  cat:nb_by_category.keySet())
		{
			double X[]=new double[nb_by_category.get(cat)];
			double Y[]=new double[nb_by_category.get(cat)];
			double Z[]=new double[nb_by_category.get(cat)];
			int pos=0;
			for(int i=0;i<learningset.size();i++)
			{
				double rcat=learningset.getLabel(i);
				v=learningset.getVector(i);
				if (rcat==cat)
				{
					X[pos]=v.getValue(0);
					Y[pos]=v.getValue(1);
					Z[pos]=0.0;
					pos++;
				}
			}
			plot.addScatterPlot("Class "+cat,X,Y,Z);
		}
		
		double[] X=new double[precision+1];
		for(int i=0;i<X.length;i++) X[i]=xmin+((xmax-xmin)/precision)*i;
		double[] Y=new double[precision+1];
		for(int i=0;i<Y.length;i++) Y[i]=ymin+((ymax-ymin)/precision)*i;
		System.out.println(xmin+" "+xmax+" "+precision+" "+((xmax-xmin)/precision));
		System.out.println(X.length);
		double Z[][]=new double[X.length][Y.length];
		for(int x=0;x<X.length;x++)
			for(int y=0;y<Y.length;y++)
			{
				Vecteur vv=new Vecteur(3);
				vv.setValue(0, X[x]);
				vv.setValue(1, Y[y]);
				vv.setValue(2, 1);
				Z[y][x]=scoringFunction.getScore(vv);
			}
		plot.addGridPlot("Decision",X,Y,Z);
		
		// put the PlotPanel in a JFrame like a JPanel
        JFrame frame = new JFrame("a plot panel");
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
	}
}
